<?php

/**
 * This is the model class for table "sps_juso_membership_changes".
 *
 * The followings are the available columns in table 'sps_juso_membership_changes':
 * @property integer $id
 * @property integer $Kundennummer
 * @property string $cancelDate
 * @property string $creationDate
 * @property integer $userId
 * @property string $userComment
 * @property string $confirmationDate
 * @property string $confirmationComment
 */
class MembershipChange extends CActiveRecord {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MembershipChange the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'sps_juso_membership_changes';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Kundennummer, Benutzernummer, Mitgliednummer, cancelDate, userComment', 'required'),
			array('Kundennummer, Benutzernummer, Mitgliednummer', 'numerical', 'integerOnly' => true),
			array('cancelDate', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'dd.MM.yyyy'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('confirmationComment', 'safe'),
			array('id, Benutzernummer, Kundennummer, cancelDate, creationDate, userId, userComment, confirmationDate, confirmationComment', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', array('userId' => 'id'), 'joinType' => 'INNER JOIN'),
			'memberdata' => array(self::BELONGS_TO, 'Member', 'Benutzernummer', 'joinType' => 'INNER JOIN'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'Kundennummer' => 'OM-Nummer',
			'Benutzernummer' => 'Benutzernummer',
			'cancelDate' => 'Austrittsdatum',
			'creationDate' => 'Datum',
			'userId' => 'User',
			'userComment' => 'Anmerkungen',
			'confirmationDate' => 'Confirmation Date',
			'confirmationComment' => 'Confirmation Comment',
			'Mitgliednummer' => 'Mitgliednummer',
			'member_name' => 'Mitglied',
		);
	}

	public function getOpenChangesByMember($member) {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');
		$criteria->compare('Benutzernummer', $member->Benutzernummer);
		return $this->find($criteria);
	}

	public function getOpenChangesByUser($userId) {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');
		$criteria->compare('userId', $userId);

		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}

	public function getOpenChanges() {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');

		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}

	public function beforeSave() {

		if ($this->isNewRecord) {
			$this->userId = Yii::app()->user->getId();
		}
		if (!empty($this->cancelDate))
			$this->cancelDate = date('Y-m-d', strtotime($this->cancelDate));
		$this->confirmationKey = UUID::generate();

		return parent::beforeSave();
	}

//	/**
//	 * Retrieves a list of models based on the current search/filter conditions.
//	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
//	 */
//	public function search()
//	{
//		// Warning: Please modify the following code to remove attributes that
//		// should not be searched.
//
//		$criteria=new CDbCriteria;
//
//		$criteria->compare('id',$this->id);
//		$criteria->compare('Kundennummer',$this->Kundennummer);
//		$criteria->compare('cancelDate',$this->cancelDate,true);
//		$criteria->compare('creationDate',$this->creationDate,true);
//		$criteria->compare('userId',$this->userId);
//		$criteria->compare('userComment',$this->userComment,true);
//		$criteria->compare('confirmationDate',$this->confirmationDate,true);
//		$criteria->compare('confirmationComment',$this->confirmationComment,true);
//
//		return new CActiveDataProvider($this, array(
//			'criteria'=>$criteria,
//		));
//	}
}